//
//  SyncGramarsDataRequest.m
//  Grammar
//
//  Created by Rick on 7/11/13.
//
//

#import "SyncGramarsDataRequest.h"
#import "Grammar.h"
#import "GrammarManager.h"

@implementation SyncGramarsDataRequest
- (NSString*)getRequestUrl
{
    return [NSString stringWithFormat:@"%@%@",REQUEST_DOMAIN,@"grammar/syncGrammars.intf?"];
}

- (ITTRequestMethod)getRequestMethod
{
    return ITTRequestMethodGet;
}

- (void)processResult
{
    NSArray *findArray = [self.resultDic objectForKey:@"syncFind"];
    NSArray *updateArray = [self.resultDic objectForKey:@"syncUpdate"];
    NSArray *deleteArray = [self.resultDic objectForKey:@"syncDelete"];
    
    for (NSDictionary *aGrammarDic in findArray) {
        [GrammarManager sharedManager].grammarType = @"normal";
        Grammar *aGrammar = [[Grammar alloc] initWithDataDic:aGrammarDic];
        [[GrammarManager sharedManager] addGrammar:aGrammar];
        [aGrammar release];
    }
    
    for (NSDictionary *aGrammarDic in updateArray) {
        [GrammarManager sharedManager].grammarType = @"normal";
        Grammar *aGrammar = [[Grammar alloc] initWithDataDic:aGrammarDic];
        [[GrammarManager sharedManager] addGrammar:aGrammar];
        [[GrammarManager sharedManager] updateGrammar:aGrammar];
        
        [GrammarManager sharedManager].grammarType = @"archive";
        [[GrammarManager sharedManager] updateGrammar:aGrammar];
        
        
        [GrammarManager sharedManager].grammarType = @"review";
        [[GrammarManager sharedManager] updateGrammar:aGrammar];
        
        [GrammarManager sharedManager].grammarType = @"normal";
        [aGrammar release];
        
    }
    
    for (NSDictionary *aGrammarDic in deleteArray) {
        [GrammarManager sharedManager].grammarType = @"normal";
        Grammar *aGrammar = [[Grammar alloc] initWithDataDic:aGrammarDic];
        [[GrammarManager sharedManager] removeGrammar:aGrammar];
        [aGrammar release];
    }
    [[DataCacheManager sharedManager] addObject:[self.resultDic objectForKey:@"wholeVersion"] forKey:WHOLE_VERSION];
}
@end
